let wxplatform = false;
let systemInfo:wx.SystemInfo = null;
if (typeof wx != "undefined"){
    wxplatform = true;
    systemInfo = wx.getSystemInfoSync();
}

export default class WxApi {
    static enable = wxplatform;
    static sysInfo = systemInfo;

    static init() {
        if (!WxApi.enable)return;
        let boj = wx.getLaunchOptionsSync();
        console.log("wx.getLaunchOptionsSync", boj);

        //wx.setKeepScreenOn({keepScreenOn:true});
        wx.showShareMenu({
            withShareTicket: false,
            success: function (code) {
                console.log("showShareMenu success");                
            },
            fail: function () {
                console.log("showShareMenu fail");
            },
            complete: function () {
            }
        });
    }

    static async checkSession(){
        return new Promise((resolve, reject) => {
            wx.checkSession({
                success: function () {
                    resolve();
                },
                fail: function () {
                    reject();
                },
                complete: function () {
                },
            });
        });
    }

    static async authorize(scope:string){
        return new Promise((resolve, reject) => {
            wx.authorize({
                scope:scope,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            })
        });
    }
    
    static login(cb) {
        wx.login({
            success: function (res) {
                cb(res.code);
            },
            fail: function (...args) {
                cb(null);
            },
            complete: function () {
            },
        })
    }

    static _getUserInfo(withCredentials:boolean, lang:string, cb) {
            wx.getUserInfo({
                withCredentials:withCredentials,
                lang:lang,
                success: function (res) {
                    cb(res);
                },
                fail: function (res) {
                    cb(res)
                }
            })
    }

    static getUserInfo(cb){
        let userInfo:object = null;
        if (WxApi.sysInfo.SDKVersion >= "2.0.1"){
            /** 创建登陆按钮 */
            WxApi.login(function (code) {
                if (code) {
                    let w = window.innerWidth * 0.52
                    let h = window.innerWidth * 0.152                    
                    let x = window.innerWidth / 2 - w / 2;
                    let y = window.innerHeight / 2 - h/2 + window.innerWidth*0.485;

                    let button = wx.createUserInfoButton({
                        type: "text",
                        text: "",
                        image: "",
                        style: {
                            left: x,
                            top: y,
                            width: w,
                            height: h,
                            lineHeight: h,
                            textAlign: "center",
                            color: "#ffffff",
                            fontSize: 44
                        }
                    });
                    /** 用户点击登陆按钮后获取userInfo */
                    button.onTap((res) => {
                        console.log("用户授权:", res);
                        button.destroy();
                        if (res.encryptedData && res.userInfo) {
                            //button.destroy();
                            res.code = code
                            cb(res);
                        }else{
                            cb(null)
                        }
                    });                    
                } else {
                    cb(null)
                }
            })
            
        }else {
            /** 直接获取userInfo,不需要授权 */
            WxApi._getUserInfo(false, "zh_CN",function () {
                cb(null)
            });
        }
    }

    static shareAppMessage(title:string,imageUrl:string,query:string) {
        wx.shareAppMessage({
            title:title,
            imageUrl:imageUrl
        })
    }
}